import { menuMapParentId } from './mapMenusRouter'
import { getPageList } from '@/service/main/system/system'

/**
 * menus type下拉框改变
 * @param val 下拉框改变后的值
 * @param config menu pageModelConfig
 * @returns type对应的parentId的options
 */
export async function menuTypeChange(val: any, config: any) {
  let options: any[] = []
  const res = await getPageList('/menu/list', {})

  if (val === '一级菜单') {
    for (const i of config.formItems) {
      if (i.field === 'permission' || i.field === 'parentId') {
        i.isHidden = true
      }
      if (i.field === 'icon' || i.field === 'url') {
        i.isHidden = false
      }
    }
  } else if (val === '二级菜单') {
    options = menuMapParentId(res.data.list, 1)
    for (const i of config.formItems) {
      if (i.field === 'icon' || i.field === 'permission') {
        i.isHidden = true
      }
      if (i.field === 'url' || i.field === 'parentId') {
        i.isHidden = false
      }
    }
  } else if (val === '三级菜单') {
    options = menuMapParentId(res.data.list, 2)
    for (const i of config.formItems) {
      if (i.field === 'icon' || i.field === 'url') {
        i.isHidden = true
      }
      if (i.field === 'permission' || i.field === 'parentId') {
        i.isHidden = false
      }
    }
  }
  return options
}
